﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneTariffList : UserControl
    {
        private VodafoneController _vodafoneController;

        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                _vodafoneController.TariffSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public VodafoneTariffList()
        {
            InitializeComponent();
            editToolStripMenuItem.Click += editToolStripMenuItem_Click;
            addToolStripMenuItem.Click += addToolStripMenuItem_Click;
            gridView.DoubleClick += gridView_DoubleClick;
            contextMenuStrip.Opening += contextMenuStrip_Opening;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            gridControl.DataSource = _vodafoneController.Tariffs;
        }

        private static Tarifario GetClone(Tarifario tariff)
        {
            Tarifario document = new Tarifario();
            document.IdTarifario = tariff.IdTarifario;
            document.Nome = tariff.Nome;
            document.Activo = tariff.Activo;
            return document;
        }

        private static void Merge(Tarifario tariff, Tarifario document)
        {
            tariff.Nome = document.Nome;
            tariff.Activo = document.Activo;
        }

        private void ShowTariff(Tarifario tariff)
        {
            Tarifario document = GetClone(tariff);
            VodafoneTariff window = new VodafoneTariff();
            window.Document = document;
            if (DialogResult.OK != window.ShowDialog())
                return;
            Merge(tariff, window.Document);
            _vodafoneController.SaveTariff(tariff);
        }

        #endregion

        void gridView_DoubleClick(object sender, EventArgs e)
        {
            Tarifario client = (Tarifario)gridView.GetRow(gridView.FocusedRowHandle);
            if (client == null)
                return;
            ShowTariff(client);
        }

        void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tarifario tariff = (Tarifario)gridView.GetRow(gridView.FocusedRowHandle);
            ShowTariff(tariff);
        }

        void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tarifario tariff = new Tarifario();
            tariff.IdTarifario = -1;
            tariff.Nome = string.Empty;
            ShowTariff(tariff);
        }

        void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            Tarifario tariff = (Tarifario)gridView.GetRow(gridView.FocusedRowHandle);
            editToolStripMenuItem.Enabled = tariff != null;
        } 
    }
}
